package wh.两数相加;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/3 10:39
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(0,new ListNode(1));
        ListNode node2 = new ListNode(7,new ListNode(3,new ListNode(2)));
        addTwoNumbers2(node1,node2);
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode p = l1;
        int count = 0;
        while (l1 != null && l2 != null){
            int sum = l1.val + l2.val + count;
            if (sum < 10 ){
                l1.val = sum;
                count = 0;
            }else {
                int mod = sum % 10;
                l1.val = mod;
                count = 1;
            }
            l1 = l1.next;
            l2 = l2.next;
        }

        while (l1 != null){
            if (l1.val+count != 10){
                l1.val += count;
                count = 0;
                break;
            }else {
                l1.val = 0;
                count = 1;
            }
            l1 = l1.next;
        }

        ListNode q = p;
        while (q.next != null){
            q = q.next;
        }
        while (l2 != null){
            if (l2.val+count != 10){
                l2.val += count;
                count = 0;
            }else {
                l2.val = 0;
                count = 1;
            }
            q.next = l2;
            q = q.next;
            l2 = l2.next;
        }

        if (count == 1){
            ListNode lastNode = new ListNode(1);
            q.next = lastNode;
        }

        return p;
    }

    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2){
        int num1 = 0;
        int num2 = 0;
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        while (l1 != null){
            list1.add(l1.val);
            l1 = l1.next;
        }
        while (l2 != null){
            list2.add(l2.val);
            l2 = l2.next;
        }
        for (int i = list1.size() - 1; i >= 0; i--) {
            num1 += list1.get(i)*Math.pow(10,i);
        }
        for (int i = list2.size() - 1; i >= 0; i--) {
            num2 += list2.get(i)*Math.pow(10,i);
        }
        Integer sum = num1 + num2;
        char[] chars = sum.toString().toCharArray();

        ListNode p = new ListNode();
        ListNode q = new ListNode();
        for (int i = chars.length - 1; i >= 0 ; i--) {
            if (i == chars.length - 1){
                ListNode node = new ListNode(Character.getNumericValue(chars[i]));
                p = node;
                q = p;
            }else {
                p.next = new ListNode(Character.getNumericValue(chars[i]));
                p = p.next;
            }
        }
        return q;
    }

    public static ListNode addTwoNumbers3(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre;
        int carry = 0;
        while(l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + carry;

            carry = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);

            cur = cur.next;
            if(l1 != null)
                l1 = l1.next;
            if(l2 != null)
                l2 = l2.next;
        }
        if(carry == 1) {
            cur.next = new ListNode(carry);
        }
        return pre.next;
    }
}
